package com.sbtr.util;

import com.sbtr.exception.CustException;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class ImageCropUtils {

	private ImageCropUtils() {
	}

	/**
	 * 多边形裁剪
	 * 
	 * @param srcImage      需要裁剪的图
	 * @param xPoints       组成多边形坐标的X
	 * @param yPoints       组成多边形左边的Y
	 * @param outPath       裁剪图保存输出路径
	 * @param isTransparent 多边形裁剪图底色是否透明
	 * @return
	 * @throws Exception
	 */
	public static boolean cropPolygonImage(BufferedImage srcImage, int[] xPoints, int[] yPoints, String outPath,
			boolean isTransparent) throws Exception {
		int sw = srcImage.getWidth();
		int sh = srcImage.getHeight();
		int minX = Integer.MAX_VALUE;
		int maxX = 0;
		int minY = Integer.MAX_VALUE;
		int maxY = 0;
		for (int i = 0; i < xPoints.length; i++) {
			if (minX > xPoints[i]) {
				minX = xPoints[i];
			}
			if (maxX < xPoints[i]) {
				maxX = xPoints[i];
			}
			if (xPoints[i] > sw) {
				throw new CustException("多边形参数输入错误");
			}
		}
		for (int i = 0; i < yPoints.length; i++) {
			if (minY > yPoints[i]) {
				minY = yPoints[i];
			}
			if (maxY < yPoints[i]) {
				maxY = yPoints[i];
			}
			if (yPoints[i] > sh) {
				throw new CustException("多边形参数输入错误");
			}
		}
		int ow = maxX - minX;
		int oh = maxY - minY;
		if (isTransparent) {
			BufferedImage maskImage = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_ARGB);
			BufferedImage outImage = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_ARGB);

			Graphics2D dstG2d = maskImage.createGraphics();
			dstG2d.setColor(Color.green);
			dstG2d.fillPolygon(xPoints, yPoints, xPoints.length);
			dstG2d.dispose();

			Graphics2D outG2d = outImage.createGraphics();
			outG2d.setComposite(AlphaComposite.Src);
			outG2d.drawImage(srcImage, 0, 0, null); // 背景图

			outG2d.setComposite(AlphaComposite.DstIn);
			outG2d.drawImage(maskImage, 0, 0, null);
			return cropRectangleImage(outImage, minX, minY, ow, oh, outPath);
		} else {
			return cropRectangleImage(srcImage, minX, minY, ow, oh, outPath);
		}
	}

	/**
	 * 矩形裁剪
	 * 
	 * @param srcImage 需要裁剪的图
	 * @param startX   距离左边的偏移量裁剪
	 * @param startY   距离上边的偏移量裁剪
	 * @param w        裁剪宽度
	 * @param h        裁剪高度
	 * @param outPath  裁剪图保存输出路径
	 * @return
	 * @throws Exception
	 */
	public static boolean cropRectangleImage(BufferedImage srcImage, int startX, int startY, int w, int h,String outPath) throws Exception {
		if (null == srcImage || w < 0 || h < 0 || null == outPath) {
			throw new CustException("参数输入错误");
		}
		int sw = srcImage.getWidth();
		int sh = srcImage.getHeight();
		if ((startX + w) > sw || (startY + h) > sh) {
			throw new CustException("参数输入错误");
		}
		BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		int[] rgbArray = srcImage.getRGB(startX, startY, w, h, null, 0, w);
		outImage.setRGB(0, 0, w, h, rgbArray, 0, w);
		return ImageIO.write(outImage, "png", new FileImageOutputStream(new File(outPath)));
	}
}
